#include "UtilStr.h"
#include <time.h>




void copStr(char ** dest, const char * src)
{
  if((*dest) != NULL)
    {
      free((*dest));
      (*dest) = NULL;
    }
  if(src)
    {
      (*dest) = (char *) malloc ((1 + strlen(src)) * sizeof(char));
      if((*dest) == NULL)
	{
	  fprintf(stderr, "Mémoire insuffisante");
	  exit(EXIT_FAILURE);
	}
      strcpy((*dest), src);
    }
}

void detStr(char ** uti)
{
  if((*uti))
    {
      free((*uti));
      (*uti) = NULL;
    }
}


char *substr(char *src,int pos,int len)
    {
    char *dest=NULL;
    if (len>0)
        {
        dest = (char *) malloc(len+1);
        strncat(dest,src+pos,len);
        }
    return dest;
    }

char *str_join (char *cs, ...)
    {
    va_list va;
    const char *ct;
    char *s = NULL;
    size_t size = 0;

    va_start (va, cs);
    /* (1) */
    while ((ct = va_arg (va, char *)) != NULL)
        {
        void *tmp = NULL;

        /* (2) */
        size += strlen (ct) + strlen (cs);
        tmp = realloc (s, sizeof (*s) * (size + 1));
        if (tmp != NULL)
            {
            if (s == NULL)
                {
                /* (3) */
                s = tmp;
                strcpy (s, ct);
                }
            else
                {
                /* (4) */
                s = tmp;
                strcat (s, cs);
                strcat (s, ct);
                }
            }
        else
            {
            fprintf (stderr, "Memoire insuffisante\n");
            free (s);
            s = NULL;
            exit (EXIT_FAILURE);
            }
        }
    return s;
    }

char **str_split (char *s, const char *ct)
    {
    char **tab = NULL;

    if (s != NULL && ct != NULL)
        {
        int i;
        char *cs = NULL;
        size_t size = 1;

        /* (1) */
        for (i = 0; (cs = strtok (s, ct)); i++)
            {
            if (size <= i + 1)
                {
                void *tmp = NULL;

                /* (2) */
                size <<= 1;
                tmp = realloc (tab, sizeof (*tab) * size);
                if (tmp != NULL)
                    {
                    tab = tmp;
                    }
                else
                    {
                    fprintf (stderr, "Memoire insuffisante\n");
                    free (tab);
                    tab = NULL;
                    exit (EXIT_FAILURE);
                    }
                }
            /* (3) */
            tab[i] = cs;
            s = NULL;
            }
        tab[i] = NULL;
        }
    return tab;
    }


int estUneDateDeLaSemaine(char * date)
{
if(date==NULL)return -1;
char*   date_1=calloc(40,sizeof(char));
strcpy(date_1,date);
char **tabDate=str_split(date_1,"--");
int annee=atoi(tabDate[0]);
int mois=atoi(tabDate[1]);
int jour=atoi(tabDate[2]);





    time_t timestamp;
    struct tm * t;

    timestamp = time(NULL);
    t = localtime(&timestamp);




    if(annee==t->tm_year+1900 && mois==t->tm_mon+1 && jour<=(t->tm_mday+4) && jour>=(t->tm_mday-4))
	{
        free(date_1);
		return 0;
	}
        free(date_1);
	return -1;
    /* Affiche la date et l'heure courante (format francais) */
	/*
    printf("Nous sommes %s, ", NomJourSemaine[t->tm_wday]);
    printf("le %02u %s %04u, ", t->tm_mday, NomMois[t->tm_mon], 1900 + t->tm_year);
    printf("et il est %02uh %02umin %02usec.\n", t->tm_hour, t->tm_min, t->tm_sec);
    */




}

/** @brief detTab
  *
  * @todo: document this function
  */
void detTab(void ** tab)
{
    if(*tab==NULL)return;
    TabGenerique    *   tabGen=*tab;
    int i=0;
    while(i<tabGen->nbElt)
    {
        free(tabGen->tab[i]);
        i++;
    }
    free(&(tabGen->tab));
    tabGen->tab=NULL;
    tabGen->nbElt=0;
    tab=NULL;
    tabGen=NULL;
}


